Skip to content

Create reversesort #11886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Conversation

Shwetasingh77
Copy link

Time Complexity: The time complexity of this algorithm is still O(n²), similar to traditional selection sort, since it involves finding the minimum element in the unsorted portion and reversing a subarray.
Unique Feature: The reversal of subarrays distinguishes this algorithm from traditional selection sort, providing an interesting approach to sorting.

Describe your change:

Explanation:
Find the Minimum: For each position i in the array, find the minimum element in the unsorted portion.
Reverse Subarray: After finding the minimum element, reverse the subarray starting from i up to the position of the minimum element (min_index).
Repeat: This process repeats for each subsequent position, progressively sorting the array.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Time Complexity: The time complexity of this algorithm is still O(n²), similar to traditional selection sort, since it involves finding the minimum element in the unsorted portion and reversing a subarray.
Unique Feature: The reversal of subarrays distinguishes this algorithm from traditional selection sort, providing an interesting approach to sorting.
@algorithms-keeper algorithms-keeper bot added require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 9, 2024
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@@ -0,0 +1,29 @@
def reverse_selection_sort(arr):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/reversesort.py, please provide doctest for the function reverse_selection_sort

Please provide return type hint for the function: reverse_selection_sort. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: arr

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type hinting: For the function parameter arr and the return type.
Doctests: To validate the functionality.
Updated Code with Type Hinting and Doctests:
from typing import List

def reverse_selection_sort(arr: List[int]) -> List[int]:
"""
Sorts an array using a modified selection sort algorithm where after finding
the minimum element, a subarray is reversed instead of swapping.

Parameters:
arr (List[int]): The list of integers to sort.

Returns:
List[int]: The sorted list of integers.

Example:
>>> reverse_selection_sort([64, 25, 12, 22, 11])
[11, 12, 22, 25, 64]

>>> reverse_selection_sort([5, 4, 3, 2, 1])
[1, 2, 3, 4, 5]

>>> reverse_selection_sort([3, 1, 2])
[1, 2, 3]

>>> reverse_selection_sort([10])
[10]
"""

n = len(arr)

# Iterate over each position of the array
for i in range(n - 1):
    # Find the index of the minimum element in the unsorted portion
    min_index = i
    for j in range(i + 1, n):
        if arr[j] < arr[min_index]:
            min_index = j

    # If the minimum is not already at position i, reverse the subarray
    if min_index != i:
        # Reverse the subarray from position i to min_index
        arr[i:min_index + 1] = reversed(arr[i:min_index + 1])

return arr

if name == "main":
import doctest
doctest.testmod()
Explanation:
Type Hinting:

The parameter arr is typed as List[int] to indicate that it expects a list of integers.
The return type is List[int], as the function returns the sorted list.
Doctests:

Doctests are added inside the docstring of the function to provide examples and automatically test the function.
Example inputs and expected outputs are provided in the docstring using >>>.
If you run this code, the doctest module will check if the output matches the expected results from the examples.
How to Run the Code:
You can run the script as is, and it will automatically run the tests using doctest. If all tests pass, there will be no output. If any test fails, doctest will show which test failed.

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 9, 2024
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 9, 2024
@algorithms-keeper algorithms-keeper bot removed require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 9, 2024
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant